import { _decorator, Component, Label, Node, ProgressBar,Vec3,director,assetManager, AssetManager, Asset } from 'cc';
const { ccclass, property } = _decorator;

@ccclass
export default class LoadingController extends Component {

    @property({type: ProgressBar})
    progressBar: ProgressBar = null;

    @property({type: Node})
    LoadingLabelNode: Node = null;

    @property({type:Label})
    percentageLabel: Label = null;

    // 加载进度文字位移距离,使进度文字与进度条一起前进
    private loadingLabel = 1.6

    // 总加载进度
    private AllLoading = 0
    // 次加载进度
    private loading1 = 0
    private loading2 = 0
    private loading3 = 0
    start(){
        // director.preloadScene("scene",(completedCount:number,totalCount:number,item:any) =>{

        //     let progress = completedCount/totalCount;

        //     console.log(progress)

        // },()=>{

        //     // this.loadRes();

        //     console.log("主界面预加载完成");

        //     director.loadScene("scene");

        // });

        const bundle1 = assetManager.loadBundle('prefab', (error: Error, bundle: AssetManager.Bundle) => {
            if (error) {
                console.error(error);
                return;
            }
            bundle.loadDir('', 
                (completedCount: number, totalCount: number) => {
                    let progress = 0
                    // 获取加载进度
                    progress = completedCount / totalCount;
                    // 一共加载三个文件夹，分三部分获取总加载进度
                    this.AllLoading += 33.33 * (progress - this.loading1)
                    this.loading1 = progress
                    console.log(progress,"1")
                    if(progress >= 1){
                        return
                    }
                },
                (error: Error) => {
                    if (error) {
                        console.error(error);
                        return;
                    }
                }
            );
        });

        const bundle2 = assetManager.loadBundle('resource', (error: Error, bundle: AssetManager.Bundle) => {
            if (error) {
                console.error(error);
                return;
            }
            bundle.loadDir('', 
                (completedCount: number, totalCount: number) => {
                    let progress = 0
                    progress = completedCount / totalCount;
                    console.log(totalCount,"2")
                    this.AllLoading += 33.33 * (progress - this.loading2)
                    this.loading2 = progress
                    console.log(progress,"2")
                    if(progress >= 1){
                        // this.AllLoading += 33.33
                        // console.log(progress,"2")
                        return
                    }
                },
                (error: Error) => {
                    if (error) {
                        console.error(error);
                        return;
                    }
                }
            );
        });

        const bundle3 = assetManager.loadBundle('scene', (error: Error, bundle: AssetManager.Bundle) => {
            if (error) {
                console.error(error);
                return;
            }
            bundle.loadDir('', 
                (completedCount: number, totalCount: number) => {
                    let progress = 0
                    progress = completedCount / totalCount;
                    this.AllLoading += 33.33 * (progress - this.loading3)
                    this.loading3 = progress
                    console.log(progress,"3")
                    if(progress >= 1){
                        // this.AllLoading += 33.33
                        // console.log(progress,"3")
                        return
                    }
                },
                (error: Error) => {
                    if (error) {
                        console.error(error);
                        return;
                    }
                }
            );
        });
        
    }

    update(dt: number) {
        // 当进度达到100%时调整新界面
        this.percentageLabel.string = `加载进度: ${(this.AllLoading).toFixed(1)}%`
        this.progressBar.progress = this.AllLoading / 100
        if(this.AllLoading >= 1){
            // director.loadScene("Game");
            // 直接return，不然进度条会继续往前走
            return
        }
        
        
    }

}